package com.hainatong.shiphelper.module.warehouse.fragment;


import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentLinyongRecordBinding;
import com.hainatong.shiphelper.module.warehouse.adapter.EquipmentRecordAdapter;
import com.hainatong.shiphelper.module.warehouse.adapter.WorkerRecordAdapter;
import com.hainatong.shiphelper.module.warehouse.LinYongDetailActivity;
import com.hainatong.shiphelper.module.warehouse.entity.WorkerRecordBean;
import com.hainatong.shiphelper.module.warehouse.viewmodel.LinYongRecordViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.shiphelper.utils.RecyclerViewHelper;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;

/**
 *  领用记录
 */
public class LinYongRecordFragment extends MvvmLazyFragment<FragmentLinyongRecordBinding, LinYongRecordViewModel>
{

    private WorkerRecordAdapter workerAdapter;
    private EquipmentRecordAdapter equipmentAdapter;


    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_linyong_record;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }


    @Override
    protected void initView(View view)
    {
        workerAdapter = new WorkerRecordAdapter();
        equipmentAdapter = new EquipmentRecordAdapter();

        RecyclerViewHelper.addLinerLayoutMgrNoDecoration( viewDataBinding.recyclerview );
        viewDataBinding.recyclerview.setAdapter( workerAdapter );

        workerAdapter.setOnItemClickListener( (adapter, view1, position) -> {

            WorkerRecordBean mWorker = workerAdapter.getItem( position );

            LinYongDetailActivity.startByParam(getActivity(), GsonUtil.obj2json( mWorker ));

        } );
        viewDataBinding.refreshlayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        viewDataBinding.refreshlayout.setRefreshFooter( new ClassicsFooter(getActivity()));
        viewDataBinding.refreshlayout.setOnRefreshListener(refreshLayout -> {

            viewDataBinding.refreshlayout.finishRefresh(true);

        });
        viewDataBinding.refreshlayout.setOnLoadMoreListener(refreshLayout -> {
            viewDataBinding.refreshlayout.finishLoadMore(true);
        });
        //搜索按钮
        viewDataBinding.btnSearch.setOnClickListener(v -> {
            // 搜索
            String keyWord = viewDataBinding.etSearch.getText().toString().trim();
            if(ObjectUtils.isNotEmpty( keyWord ))
            {
                queryLinYongRecordByName( keyWord );
            }
        });
        // 软键盘 上的 搜索 按钮
        viewDataBinding.etSearch.setOnKeyListener( (v, keyCode, event) -> {
            if(keyCode == KeyEvent.KEYCODE_ENTER)
            {
                String keyWordStr = viewDataBinding.etSearch.getText().toString().trim();
                if(!TextUtils.isEmpty( keyWordStr ))
                {
                    KeyboardUtils.hideSoftInput( getActivity() );
                    queryLinYongRecordByName( keyWordStr );
                }
                return  true;
            }
            return false;
        } );


    }

    @Override
    protected void lazyLoad()
    {
        //默认 搜索
        queryLinYongRecordByName("");

        viewModel.getEmployerResult().observe( this, result -> {

            if( result.isSuccess() )
            {
                if(ObjectUtils.isNotEmpty( result.getData() ) && !result.getData().isEmpty() )
                {
                    workerAdapter.setList( result.getData() );
                }
                // 空
            }
            else
            {
                ToastUtils.showShort( result.getMsg() );
            }
        } );
    }

    private void queryLinYongRecordByName(String name)
    {
        viewModel.queryLinYongUserListByName(name);
    }


}
